/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI_ProcesarDocumento.java
 *
 * Created on 22/10/2011, 10:54:45 AM
 */
package swatsoft3.vista;

import javax.swing.*;
import swatsoft3.controlador.*;
import swatsoft3.modelo.*;

/**
 *
 * @author renzo
 */
public class GUI_ProcesarDocumento extends javax.swing.JDialog {

    /** Creates new form GUI_ProcesarDocumento */
    public GUI_ProcesarDocumento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCarpeta = new javax.swing.JTextField();
        btnCarpeta = new javax.swing.JButton();
        btnProcesar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        lblTotalOmitidos = new javax.swing.JLabel();
        lblTiempo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDocsAnalizados = new javax.swing.JTextArea();
        lblTotalProcesados = new javax.swing.JLabel();
        lblEstado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SWATSoft - Procesar documentos");
        setMinimumSize(new java.awt.Dimension(560, 630));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtCarpeta.setEditable(false);
        getContentPane().add(txtCarpeta, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 130, 420, -1));

        btnCarpeta.setText("...");
        btnCarpeta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarpetaActionPerformed(evt);
            }
        });
        getContentPane().add(btnCarpeta, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 130, 30, 30));

        btnProcesar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/swatsoft3/imagenes/servidor-icono-mini.png"))); // NOI18N
        btnProcesar.setText("Procesar documentos");
        btnProcesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcesarActionPerformed(evt);
            }
        });
        getContentPane().add(btnProcesar, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 180, -1, -1));

        jLabel2.setText("cuyo nombre coincida con uno ya existente en la base.");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, -1, -1));

        jLabel4.setText("Documentos procesados:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 270, -1, -1));

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/swatsoft3/imagenes/man-cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 180, 220, -1));

        lblTotalOmitidos.setText("Total omitidos:");
        getContentPane().add(lblTotalOmitidos, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 490, -1, -1));

        lblTiempo.setText("Tiempo de procesamiento:");
        getContentPane().add(lblTiempo, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 520, -1, -1));

        jLabel1.setText("Por favor, elija una carpeta con los documentos que desea etiquetar,");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, -1, -1));

        jLabel3.setText("para poder agregarlos a la base de datos. Se ignorará los documentos");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, -1, -1));

        txtDocsAnalizados.setColumns(1);
        txtDocsAnalizados.setEditable(false);
        txtDocsAnalizados.setRows(100);
        txtDocsAnalizados.setText("Resultados:");
        jScrollPane1.setViewportView(txtDocsAnalizados);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 300, 470, 170));

        lblTotalProcesados.setText("Total procesados:");
        getContentPane().add(lblTotalProcesados, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 490, -1, -1));

        lblEstado.setText("...");
        getContentPane().add(lblEstado, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 270, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCarpetaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarpetaActionPerformed
// TODO add your handling code here:

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int seleccion = fileChooser.showOpenDialog(this);
    if (seleccion == JFileChooser.APPROVE_OPTION) {
        this.txtCarpeta.setText(fileChooser.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_btnCarpetaActionPerformed

private void btnProcesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcesarActionPerformed
// TODO add your handling code here:
    long tiempoInicio = System.currentTimeMillis();
    this.lblEstado.setText("Procesando...");
    BE_TString error = new BE_TString();
    BL_AdministradorDeReportes.procesarDocumento(this.txtCarpeta.getText(), error, this.txtDocsAnalizados, this.lblTotalProcesados, this.lblTotalOmitidos);
    if (error.getCadena().compareTo("") != 0) {
        JOptionPane.showMessageDialog(this, error.getCadena());
    }
    else{
    //Se divide entre mil por que son milisegundos
    long totalTiempo = (System.currentTimeMillis() - tiempoInicio) / 1000;
    int totalRedondeado = (int)Math.rint(totalTiempo);
    this.lblTiempo.setText("Tiempo de procesamiento: "+ totalRedondeado +" segundos.");
    }
    this.lblEstado.setText("Terminado.");
}//GEN-LAST:event_btnProcesarActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed
    /**
     * @param args the command line arguments
     */
    /*
    public static void main(String args[]) {
    
    java.awt.EventQueue.invokeLater(new Runnable() {
    
    public void run() {
    GUI_ProcesarDocumento dialog = new GUI_ProcesarDocumento(new javax.swing.JFrame(), true);
    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    
    @Override
    public void windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCarpeta;
    private javax.swing.JButton btnProcesar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblTiempo;
    private javax.swing.JLabel lblTotalOmitidos;
    private javax.swing.JLabel lblTotalProcesados;
    private javax.swing.JTextField txtCarpeta;
    private javax.swing.JTextArea txtDocsAnalizados;
    // End of variables declaration//GEN-END:variables
}
